<?php


namespace app\api\exception;


use app\common\lib\Show;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\Response;
use Throwable;

class HttpBase extends Handle
{
    private $httpCode = 500;

    public function render($request, Throwable $e): Response
    {
        // HttpException异常
        if ($e instanceof HttpException){
            return Show::error($e->getMessage(),$e->getStatusCode());
        }
        // 验证失败异常
        if ($e instanceof ValidateException){
            return Show::error($e->getError()['msg'],$e->getError()['code']);
        }

        if (method_exists($e, 'getStatusCode')){
            $this->httpCode = $e->getStatusCode();
        }
        // 添加自定义异常处理机制
        return Show::error($e->getMessage(), $this->httpCode);
    }
}
